//
//  SightDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "SightDetailViewController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "CityDetailViewController.h"

@interface SightDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;

@end

@implementation SightDetailViewController


- (void)dealloc
{
    [_table release];
    [_dic release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginConnection];

     self.title = @"全部城市";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
   
    
}
- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)beginConnection
{//http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=11&lat=38.8826887350744&lon=121.5392263350393&page=1&track_app_channel=App%2520Store&track_app_version=6.6&track_device_info=iPhone%25205s&track_deviceid=2CC19978-A0E5-49F2-9719-59DC5EDE6BBB&track_os=ios%25208.4&v=1
    
    
    
    NSDictionary *dic = @{@"lat":@"38.8826887350744",
                          @"lon":@"121.5392263350393",
                          @"countryid":self.sid,
                          @"page":@"1",
                          @"v":@"1",
                          @"count":@"20",
                          @"client_id":@"qyer_ios",
                          @"client_secret":@"cd254439208ab658ddf9",
                          @"track_os":@"ios%25208.4",
                          @"track_deviceid":@"2CC19978-A0E5-49F2-9719-59DC5EDE6BBB",
                          @"track_device_info":@"iPhone%25205s",
                          @"track_app_version":@"6.6"
                          };
        [AsyncConnection asyncConnection:CLASS_SIGHT parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            self.array = [dic objectForKey:@"data"];
        [self createTable];
    }path:self.sid];
}


- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 70) style:UITableViewStylePlain];
    [self.view addSubview:self.table ];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];
//    [self createButton];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.row]];
    }
            UIImageView *imag = [[UIImageView alloc]initWithFrame:CGRectMake(5 *_offWidth, 5 *_offHeight, self.view.frame.size.width - 10 *_offWidth, 240 *_offHeight)];
            [cell.contentView addSubview:imag];
            [imag sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:indexPath.row] objectForKey:@"photo"]] placeholderImage:nil];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 200 *_offWidth) / 2, 100*_offHeight, 200 *_offWidth, 40*_offHeight)];
        [imag addSubview:label];
        label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:30];
        label.textAlignment = 1;
        label.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"catename"];
        [label release];
    
    UILabel *english = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 100 *_offWidth) / 2, 140*_offHeight, 100 *_offWidth , 20 *_offHeight)];
    [imag addSubview:english];
    english.textColor = [UIColor whiteColor];
    
    english.textAlignment = 1;
    english.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"catename_en"];
    [english release];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250 *_offHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    CityDetailViewController *city = [[CityDetailViewController alloc]init];
    city.cityId = [[self.array objectAtIndex:indexPath.row]objectForKey:@"id"];
    [self.navigationController pushViewController:city animated:YES];
//    [city release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
